﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace frmCheckStock
{
    public partial class frmCheckingStock : Form
    {
        LogicUniversityT2Entities logic = new LogicUniversityT2Entities();

        public frmCheckingStock()
        {
            InitializeComponent();
        }

        private void frmCheckingStock_Load(object sender, EventArgs e)
        {
            var categoryData = logic.Categories;
            cboCategoryName.DataSource = categoryData;
            cboCategoryName.DisplayMember = "CategoryName";
        }

        private void cboCategoryName_SelectedIndexChanged(object sender, EventArgs e)
        {

            Category c = (Category)cboCategoryName.SelectedItem;

            var itemList = from item in logic.Items
                           where item.CategoryID == c.CategoryID
                           select new
                           {
                               Items_ID = item.ItemID,
                               Items_Description = item.ItemDescription,
                           };
            var itemlist = itemList.ToList();
            gvItemList.DataSource = itemlist;
        }

        private void btnSaveItems_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvItemList.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell chbox1 = new DataGridViewCheckBoxCell();

                chbox1 = (DataGridViewCheckBoxCell)gvItemList.Rows[i].Cells[0];
                if (chbox1.Value == null)
                    chbox1.Value = false;
                if (chbox1.Value.ToString() == "")
                {
                    chbox1.Value = true;
                }
                if (Convert.ToBoolean(chbox1.Value) == true)
                {
                    AdjustmentVoucher adjVoucher = new AdjustmentVoucher();

                    int strAdjID = (logic.AdjustmentVouchers.Count())-1;
                    var a = from adid in logic.AdjustmentVouchers select adid;
                    adjVoucher = a.AsEnumerable().ElementAt<AdjustmentVoucher>(strAdjID);
                    string stringa = adjVoucher.AdjustmentID;
                    string surfixOfAdjID = stringa.Substring(0, 4);
                    string numPartOfAdjID = stringa.Substring(2);
                    int numID = int.Parse(numPartOfAdjID) + 1;
                    string newNumPartOfAdjID = Convert.ToString(numID);
                    string combineofSurfixNNum = surfixOfAdjID + newNumPartOfAdjID;

                    AdjustmentVoucher newAdjVoucher = new AdjustmentVoucher();
                    newAdjVoucher.AdjustmentID = combineofSurfixNNum.ToString();
                    DataGridViewTextBoxCell txtbox1 = new DataGridViewTextBoxCell();
                    txtbox1 = (DataGridViewTextBoxCell)gvItemList.Rows[i].Cells[1];

                    newAdjVoucher.ItemID = txtbox1.Value.ToString();
                    newAdjVoucher.Date = DateTime.Today;
                    newAdjVoucher.Quantity = 0;
                    newAdjVoucher.Status = "Pending";
                    newAdjVoucher.Remarks = "Null";
                    logic.AddToAdjustmentVouchers(newAdjVoucher);
                    logic.SaveChanges();
                }
            }
        }
        private void btnProceed_Click_1(object sender, EventArgs e)
        {
            frmStockAdjustment s = new frmStockAdjustment();
            s.Show();
        }
    }
}

